import { getGoodsType, typePageSelect } from '@/api/goodsInfo'
import { goodTypeVisibleTypeMultipleUpdate } from '@/api/goodsSet'
import { ref } from 'vue'

/**
 * 获取所有的产品分类
 */
export const useGetAllGoodTypeOptions = () => {
  const options = ref<Array<GoodTypeOptions>>([])
  const getOptionsLoading = ref(false)

  const getOptions = async () => {
    getOptionsLoading.value = true
    const res = await getGoodsType()
    getOptionsLoading.value = false
    options.value = res.data.data.rows
  }

  return {
    options,
    getOptionsLoading,
    getOptions,
  }
}

/**
 * 根据搜索条件获取产品分类
 */
export const useGetGoodTypes = () => {
  // 分类列表总条数
  const total = ref(0)

  // 获取分类加载
  const loading = ref(false)

  // 分类列表
  const goodTypes = ref<Array<GoodTypes>>([])

  // 获取分类列表请求参数
  const getGoodTypesParam = ref({
    limit: 50,
    page: 1,
    name: '',
  })

  /**
   * 获取分类
   */
  const getGoodTypes = async () => {
    loading.value = true
    const res = await typePageSelect(getGoodTypesParam.value)
    goodTypes.value = res.data.data.rows
    total.value = Number(res.data.data.total)
    loading.value = false
  }

  return {
    total,
    loading,
    goodTypes,
    getGoodTypesParam,
    getGoodTypes,
  }
}

/**
 * 可见类型批量更新
 */
export const useVisibleTypeMultipleUpdate = () => {
  // 更新加载
  const updateLoading = ref(false)

  // 更新表单
  const updateForm = ref<{ visibilityType: 1 | 2 | 3 }>({
    visibilityType: 1,
  })

  /**
   * 更新
   * @param {Array<string>} ids - 产品分类ID集合
   */
  const update = async (ids: Array<string>) => {
    updateLoading.value = true
    const res = await goodTypeVisibleTypeMultipleUpdate(ids, updateForm.value.visibilityType)
    updateLoading.value = false
    return res.data
  }

  /**
   * 重置表单
   */
  const resetForm = () => {
    updateForm.value.visibilityType = 1
  }

  return {
    updateForm,
    updateLoading,
    update,
    resetForm,
  }
}
